import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui'

// 创建一个 axios 实例
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,  // 基地址
  timeout: 10000,  // 超时时间 10 s
})

service.interceptors.request.use((config) => {
  if (store.getters.token) {
    config.headers.Authorization = `Bearer ${store.getters.token}`
  }
  return config
}, (error) => {
  return Promise.reject(error)
})


service.interceptors.response.use((response) => {
  const { data, message, success } = response.data
  if (success) {
    return data
  } else {
    // 错误提醒  error.message 
    Message.warning({
      type: 'error',
      message: message
    })
    return Promise.reject(new Error(message))
  }

}, (error) => {
  // 错误提醒  error.message 
  Message.warning({
    type: 'error',
    message: error.message
  })
  return Promise.reject(error)
})

export default service 